﻿using GalaSoft.MvvmLight;
using System.Collections.ObjectModel;
using SGT.Model;
using GalaSoft.MvvmLight.Command;

namespace SGT
{
    /// <summary>
    /// This class contains properties that a View can data bind to.
    /// <para>
    /// Use the <strong>mvvminpc</strong> snippet to add bindable properties to this ViewModel.
    /// </para>
    /// <para>
    /// You can also use Blend to data bind with the tool's support.
    /// </para>
    /// <para>
    /// See http://www.galasoft.ch/mvvm/getstarted
    /// </para>
    /// </summary>
    public class ABMMercaderiasViewModel : ViewModelBase
    {
        /// <summary>
        /// Initializes a new instance of the MercaderiasViewModel class.
        /// </summary>
        /// 
        private ObservableCollection<Mercaderia> _Mercaderias;
        private Mercaderia _MercaderiaSeleccionada;
        private Mercaderia _MercaderiaNueva;
        public RelayCommand AgregarMercaderia { get; private set; }


        public ObservableCollection<Mercaderia> Mercaderias
        {
            get { return _Mercaderias; }
            set
            {
                //esto no puede ser asi, ver de cambiar
                _Mercaderias = value;
                RaisePropertyChanged("Mercaderias");
            }
        }

        public Mercaderia MercaderiaSeleccionada
        {
            get { return _MercaderiaSeleccionada; }
            set
            {
                _MercaderiaSeleccionada = value;
                RaisePropertyChanged("MercaderiaSeleccionada");
            }
        }

        public Mercaderia MercaderiaNueva
        {
            get { return _MercaderiaNueva; }
            set
            {
                _MercaderiaNueva = value;
                RaisePropertyChanged("MercaderiaNueva");
            }
        }

        
        
        public ABMMercaderiasViewModel()
        {
            if (IsInDesignMode)
            {
            ////    // Code runs in Blend --> create design time data.
            }
            else
            {
                actualizar();
                AgregarMercaderia = new RelayCommand(NuevaMercaderia);
                MercaderiaNueva = new Mercaderia();
            }
        }

        private void NuevaMercaderia()
        {
            ManejadorDepositos.InsertarMercaderia(_MercaderiaNueva);
            actualizar();
        }

        private void actualizar()
        {
            Mercaderias = ManejadorDepositos.GetMercaderias();
            
        }

        //public override void Cleanup()
        //{
        ////    // Clean own resources if needed

        ////    base.Cleanup();
        ////}
    }
}